package com.zhongjinkang.rhythm.AppView.Activity

import android.os.Bundle
import android.util.Log
import com.zhongjinkang.rhythm.MyApplication
import com.zhongjinkang.rhythm.Utils.SPHelper
import com.zhongjinkang.rhythm.databinding.ActivityLoginBinding
import com.zhongjinkang.rhythm.viewModel.LoginViewModel
import com.zhongjinkang.rhythm.viewModel.PublicViewModel

class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>(
    LoginViewModel::class.java, ActivityLoginBinding::inflate
) {


    override fun onCreate(savedInstanceState: Bundle?) {
        SPHelper.init(this)

        if (PublicViewModel.tokenState.value == PublicViewModel.TokenState.EFFECTIVE) {
            //直接跳转
            MyApplication.instance?.appManagementUtil?.jumpToMain(this@LoginActivity)
            finish()
        }

        super.onCreate(savedInstanceState)


        onClickEvent()

    }

    override fun toRefreshUI(viedModel: LoginViewModel?) {
        viewModel?.let { it ->
            it.loginState.observe(this) { it1 ->
                if (it1 == LoginViewModel.LoginState.SUCCESS) {
                    MyApplication.instance?.appManagementUtil?.jumpToMain(this@LoginActivity)
                    finish()
                }


            }


        }
    }


    fun onClickEvent() {
        binding.btnLogin.setOnClickListener {
            PublicViewModel.loadingState.value=PublicViewModel.LoadingState.LOAD_SHOW
            it.postDelayed({
                viewModel?.let {
                    it.goToLogin()
                }
            },2000)
        }
    }
}